﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace businessLayer
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FiliaalService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select FiliaalService.svc or FiliaalService.svc.cs at the Solution Explorer and start debugging.
    public class FiliaalService : iFiliaal
    {
        public List<Filiaal> GetAlleFiliaal()
        {
            List<Filiaal> FiliaalLijst = new List<Filiaal>();
            SqlConnection myConn = null;
            Filiaal alle = new Filiaal();
            try
            {
                using (myConn = new SqlConnection(BPcars.GetConnection()))
                {
                    myConn.Open();

                    var cmd = new SqlCommand("Select * from Filiaal", myConn);
                    var reader = cmd.ExecuteReader();



                    while (reader.Read())
                    {
                        int Filiaalnr = Convert.ToInt32(reader["FiliaalNr"]);
                        string adres = Convert.ToString(reader["adres"]);
                        string postcode = Convert.ToString(reader["postcode"]);
                        string telefoon = Convert.ToString(reader["telefoon"]);
                        string email = Convert.ToString(reader["email"]);
                        int aantalMed = Convert.ToInt32(reader["aantalMed"]);

                        Filiaal k = new Filiaal { Filiaalnr = Filiaalnr, adres = adres, postcode = postcode, telefoon = telefoon, email = email, aantalMed = aantalMed };
                        FiliaalLijst.Add(k);


                    }

                    reader.Close();
                    cmd.Dispose();




                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            return FiliaalLijst;
        }

        public bool DeleteFiliaal(int id)
        {
            SqlConnection connection = new SqlConnection(BPcars.GetConnection());
            string delete = "DELETE FROM Filiaal " +
                            "WHERE Filiaalnr = @Filiaalnr"; ;
            SqlCommand deleteCommand = new SqlCommand(delete, connection);
            deleteCommand.Parameters.AddWithValue("@Filiaalnr", id);
            try
            {
                connection.Open();
                int count = deleteCommand.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }

        public bool InsertFiliaal(Filiaal filiaal)
        {
           
            SqlConnection connection = null;
            string insert = "insert into Filiaal (Filiaalnr,adres,postcode,telefoon,email, aantalMed)" +
                            "Values(@FiliaalNr,@adres,@postcode,@telefoon,@email,@aantalMed);";
            SqlCommand insertCommand = new SqlCommand(insert, connection);
            insertCommand.Parameters.AddWithValue("@Filiaalnr", filiaal.Filiaalnr);
            insertCommand.Parameters.AddWithValue("@adres", filiaal.adres);
            insertCommand.Parameters.AddWithValue("@postcode", filiaal.postcode);
            insertCommand.Parameters.AddWithValue("@telefoon", filiaal.telefoon);
            insertCommand.Parameters.AddWithValue("@email", filiaal.email);
            insertCommand.Parameters.AddWithValue("@aantalMed", filiaal.aantalMed);
            
            try
            {
                using (connection = new SqlConnection(BPcars.GetConnection()))
                {
                    connection.Open();
                    int count = insertCommand.ExecuteNonQuery();
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (SqlException ex)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
